package com.mall.shopping.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.services.cache.CacheManager;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;

/**
 * @program: cs-mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-21 15:18
 **/
@Slf4j
@Component
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    CacheManager cacheManager;

    @Override
    public HomePageResponse homepage(){
        HomePageResponse response = new HomePageResponse();
        ObjectMapper mapper = new ObjectMapper();
        try {
            // 如果redis中没有对应的数据，就去数据库里查并存入redis中
            if (cacheManager.checkCache("panelDtos")==null) {
                // 获取主页显示的所有接口
                Example example = new Example(Panel.class);
                example.createCriteria().andEqualTo("position", 0)
                        .andEqualTo("status", 1);
                List<Panel> panels = panelMapper.selectByExample(example);
                HashSet<PanelDto> panelDtos = new HashSet<>();
                for (Panel panel : panels) {
                    // 根据每个接口的id，查询子类目
                    List<PanelContentItem> panelContentItems =
                            panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                    panel.setPanelContentItems(panelContentItems);
                    PanelDto panelDto = contentConverter.panen2Dto(panel);
                    panelDtos.add(panelDto);
                }


                response.setPanelContentItemDtos(panelDtos);
                // 存入json字符串
                String panelsD = mapper.writeValueAsString(panelDtos);
                cacheManager.setCache("panelDtos",panelsD,1);

                System.out.println("从数据库中获取的"+panelDtos.getClass().getName());
            } else {
                // 获取json字符串
                String cache = cacheManager.checkCache("panelDtos");

                TypeFactory typeFactory = mapper.getTypeFactory();
                CollectionType collectionType = typeFactory.constructCollectionType(HashSet.class, PanelDto.class);
                HashSet<PanelDto> panelDtos = mapper.readValue(cache, collectionType);

                response.setPanelContentItemDtos(panelDtos);
                System.out.println("从redis中获取的"+panelDtos.getClass().getName());
            }
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("HomeServiceImpl.homepage occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
